import { Component } from '@angular/core';
import { Http } from '@angular/http';
import { ActivatedRoute } from '@angular/router';
//Base components
import { AnalystViewModel } from '../../../shared/templates/analystView/analystView.model';
//Services
import { CompanyRiskService } from '../../companyRisk.service';

declare var Modernizr: any;
declare var $: any;

@Component({
    selector: 'mei-analyst-view',
    templateUrl: '../../../shared/templates/analystView/analystView.component.html',
    styleUrls: ['../../../shared/templates/analystView/analystView.component.css']
})
export class AnalystViewMeiComponent extends AnalystViewModel {
    private sub: any;

    constructor(
        private route: ActivatedRoute,
        private riskService: CompanyRiskService,
        private http: Http) {
        super(http);
        this.isMei = true;
    }

    ngOnInit() {
        this.sub = this.route.params.subscribe(params => {
            this.load();
        });
    }

    ngOnDestroy() {
        this.sub.unsubscribe();
    }

    private load(): void {
        this.loaded = new Promise<void>((resolve, reject) => {
            this.riskService.company.then(company => {
                this.riskService.mei.then(mei => {
                    if (mei.isMain) {
                        this.exposureComment = mei.comments.exposure;
                        this.businessModelComment = mei.comments.management;
                        this.esgRiskManagement = mei.comments.analyst;
                        this.narrative = undefined;
                    }
                    else {
                        this.exposureComment = undefined;
                        this.businessModelComment = undefined;
                        this.esgRiskManagement = undefined;
                        this.narrative = mei.narrative;
                    }

                    resolve();
                });
            });
        });
    }
}